#-*- coding:utf8 -*-
    
def get_test_labels_hard():
    test_labels = []
    for ln in open("E:\\study\\nlp\\libsvm-3.0\\output", "r"):
        label = ln.strip().split()[0]
        test_labels.append(label)
    return test_labels

def comp_p_at_n(test_scores, std_labels, n):
    THRESHOLD = 0.4
    if len(test_scores)!=len(std_labels):
        print "labels num not equal! test_labels,std_labels: %d,%d" % (len(test_scores) ,len(std_labels))
        return -1
    zipped_labels = zip(test_scores, std_labels)
    sort(zipped_labels, reverse=True)
    tp_num = 0
    for i in range(min(n, len(zipped_labels))):
        if zipped_labels[i][0]>=THRESHOLD and zipped_labels[i][1]=="1":
            tp_num += 1
    return float(tp_num)/n

def comp_pr(test_fn, output_fn, threashold, exp=1):
    std_labels = []
    for ln in open(test_fn, "r"):
        label = ln.strip().split()[0]
        std_labels.append(label)
    test_labels = []
    test_scores = []
    for ln in open(output_fn, "r"):
        if ln.strip().split()[0] == "labels":
            continue
        prob = ln.strip().split()[1]
        prob = float(prob)
        label = "0"
        test_scores.append(prob)
        if prob - threashold > 0:
            label = "1"
        test_labels.append(label)
    if len(std_labels) != len(test_labels):
        print "wrong! len(std_labels), len(test_labels): %d,%d" % (len(std_labels), len(test_labels))
        return
#    print std_labels
#    print test_labels
    tp = 0
    tn = 0
    std_1_num = 0
    std_0_num = 0
    test_1_num = 0
    test_0_num = 0
    for i in range(len(std_labels)):
        if test_labels[i]=="0":
            test_0_num += 1
        elif test_labels[i]=="1":
            test_1_num += 1
        if std_labels[i]=="1":
            std_1_num += 1
            if test_labels[i]=="1":
                tp += 1
        elif std_labels[i]=="0":
            std_0_num += 1
            if test_labels[i]=="0":
                tn += 1
#    print "accuracy: ", (tp+tn)*1.0/len(test_labels)
    if exp==2 and test_0_num==0:
        print "precision: 0.0"
    elif exp==1 and test_1_num==0:
        print "precision: 0.0"
    elif exp==1:
        print "pos-precision: ", tp*1.0/test_1_num
    elif exp==2:
#        print "neg-precision: ", tn*1.0/test_0_num
        print "%f\t%f\t%d\t%d" % (tn*1.0/test_0_num,tn*1.0/std_0_num,tn,test_0_num-tn)
    if exp==1:
        print "pos-recall: ", tp*1.0/std_1_num
#    elif exp==2:
#        print "neg-recall: ", tn*1.0/std_0_num

def gen_exp2_wordlist():
    f = open("data\\top_100_exp_map.txt", "r")
    f1 = open("data\\trec78_exp2_word_list.txt", "wb")
    exp_words_dict = dict()
    for ln in f:
        q_num_str, top_n_str, exp, map_v_str = ln.strip().split()
        if int(top_n_str) >= 40:
            continue
        if not exp_words_dict.has_key(q_num_str):
            exp_words_dict[q_num_str] = []
        exp_words_dict[q_num_str].append(exp)
    for q_num_str,exp_words in exp_words_dict.items():
        size = len(exp_words)
        for i in range(size):
            for j in range(i+1, size):
                f1.write("%s\t%s\t%s\n" % (q_num_str,exp_words[i],exp_words[j]))
    f.close()
    f1.close()

def test1():
    print "#########exp1_trec7test##########"
    for td in range(0,10):
        print "threshold", float(td)/10
        comp_pr('E:\\study\\nlp\\libsvm-3.0\\test_data\\test\\trec7_exp1_test_alldata.txt', \
            'E:\\study\\nlp\\libsvm-3.0\\test_data\\exp1_trec8train_trec7test_output.txt', float(td)/10, 1)
        print ""

def test2():
    print "#########exp1_trec8test##########"
    for td in range(0,10):
        print "threshold", float(td)/10
        comp_pr('E:\\study\\nlp\\libsvm-3.0\\test_data\\test\\trec8_exp1_test_alldata.txt', \
            'E:\\study\\nlp\\libsvm-3.0\\test_data\\exp1_trec7train_trec8test_output.txt', float(td)/10, 1)
        print ""

def test3():
    print "#########exp2_trec7test##########"
    for td in range(0,10): 
#        print "threshold", float(td)/10
        comp_pr('E:\\study\\nlp\\libsvm-3.0\\test_data\\test\\trec7_exp2_test_alldata.txt', \
            'E:\\study\\nlp\\libsvm-3.0\\test_data\\exp2_trec8train_trec7test_output.txt', float(td)/10, 2)
#        print ""

def test4():
    print "#########exp2_trec8test##########"
    for td in range(0,10):
#        print "threshold", float(td)/10
        comp_pr('E:\\study\\nlp\\libsvm-3.0\\test_data\\test\\trec8_exp2_test_alldata.txt', \
            'E:\\study\\nlp\\libsvm-3.0\\test_data\\exp2_trec7train_trec8test_output.txt', float(td)/10, 2)
#        print ""

def test5():
    print "#########exp1_wt10gtest##########"
    for td in range(0,10):
        print "threshold", float(td)/10
        comp_pr('E:\\study\\nlp\\libsvm-3.0\\test_data\\test\\wt10g_exp1_alldata.txt', \
            'E:\\study\\nlp\\libsvm-3.0\\test_data\\exp1_trec78train_wt10gtest_output.txt', float(td)/10, 1)
        print ""
        
def test6():
    print "#########exp2_wt10gtest##########"
    for td in range(0,10):
#        print "threshold", float(td)/10
        comp_pr('E:\\study\\nlp\\libsvm-3.0\\test_data\\test\\wt10g_exp2_alldata.txt', \
            'E:\\study\\nlp\\libsvm-3.0\\test_data\\exp2_trec78train_wt10gtest_output.txt', float(td)/10, 2)
#        print ""

if __name__ == "__main__":
#    test1()
#    test2()
    test3()
    test4()
#    test5()
    test6()
#    comp_pr('E:\\study\\nlp\\libsvm-3.0\\test_data\\test\\trec7_exp2_test_alldata.txt', \
#            'E:\\study\\nlp\\libsvm-3.0\\test_data\\exp2_trec8train_trec7test_output.txt', 0.5, 2)
#     comp_pr('E:\\study\\nlp\\libsvm-3.0\\test_data\\test\\trec8_exp2_test_alldata.txt', \
#        'E:\\study\\nlp\\libsvm-3.0\\test_data\\exp2_trec7train_trec8test_output.txt', 0.51, 2)    
#     comp_pr('E:\\study\\nlp\\libsvm-3.0\\test_data\\test\\wt10g_exp2_alldata.txt', \
#            'E:\\study\\nlp\\libsvm-3.0\\test_data\\exp2_trec78train_wt10gtest_output.txt', 0.33, 2)
                   
